<?php
namespace jy_supplier;
defined('BY_JYA') or exit('error');
/*
	工具类
*/
class model extends \plugin{
	public function addgoods($postdata){
		$data=array(
			'title'=>trim($postdata['title']),
			'image'=>trim($postdata['image']),
			'video'=>trim($postdata['video']),
			'subtitle'=>trim($postdata['subtitle']),
			'shorttitle'=>trim($postdata['shorttitle']),
			'keywords'=>trim($postdata['keywords']),
			'saletype'=>trim($postdata['saletype']),
			'type'=>intval($postdata['type']),
			'paixu'=>intval($postdata['paixu']),
			'price'=>floatval($postdata['price']),
			'originalprice'=>floatval($postdata['originalprice']),
			'marketprice'=>floatval($postdata['marketprice']),
			'costprice'=>floatval($postdata['costprice']),
			'images'=>ijson_encode($postdata['images']),
			'quality'=>intval($postdata['quality']),
			'salenum'=>intval($postdata['salenum']),
			'unit'=>$postdata['unit']?trim($postdata['unit']):'件',
			'isshowsales'=>intval($postdata['isshowsales']),
			'status'=>intval($postdata['status']),
			'pcd'=>ijson_encode($postdata['pcd']),
			'iscashier'=>intval($postdata['iscashier']),
			'invoice'=>intval($postdata['invoice']),
			'cashpay'=>intval($postdata['cashpay']),
			'repair'=>intval($postdata['repair']),
			'seven'=>intval($postdata['seven']),
			'virtualsendcont'=>trim($postdata['virtualsendcont']),
			'virtual_template_id'=>intval($postdata['virtual_template_id']),
			'virtualsend'=>intval($postdata['virtualsend']),
			//库存
			'goodssn'=>trim($postdata['goodssn']),
			'productsn'=>trim($postdata['productsn']),
			
			'isshowtotal'=>intval($postdata['isshowtotal']),
			'total'=>intval($postdata['total']),
			'totalcnf'=>intval($postdata['totalcnf']),
			
			//参数
			'param'=>ijson_encode($postdata['param']),
			//详情
			'content'=>trim($postdata['content']),
			'buyshow'=>intval($postdata['buyshow']),
			//分享关注
			'needfollow'=>intval($postdata['needfollow']),
			'followtip'=>trim($postdata['followtip']),
			'followurl'=>trim($postdata['followurl']),
			'share_title'=>trim($postdata['share_title']),
			'share_icon'=>trim($postdata['share_icon']),
			'share_desc'=>trim($postdata['share_desc']),
			
			//卖家通知
			'noticemid'=>intval($postdata['noticemid']),
			'noticetype'=>trim($postdata['noticetype']),
			
			//核销
			'isverify'=>intval($postdata['isverify']),
			'verifytype'=>intval($postdata['verifytype']),
			'usetime'=>intval($postdata['usetime']),
			'verifygoodsnum'=>intval($postdata['verifygoodsnum']),
			'verifygoodsdays'=>intval($postdata['verifygoodsdays']),
			'verifygoodslimittype'=>intval($postdata['verifygoodslimittype']),
			'verifygoodslimitdate'=>intval(strtotime($postdata['verifygoodslimitdate'])),
			//店铺信息
			'storeids'=>ijson_encode($postdata['storeids']),
			//营销设置
			'isdiscount'=>intval($postdata['isdiscount']),
			'deductset'=>ijson_encode($postdata['deductset']),
			'fang_credit2'=>trim($postdata['fang_credit2']),
			//自定义表单
			'diyfields'=>$postdata['diyfields']?ijson_encode($postdata['diyfields']):'',
			'diyformid'=>intval($postdata['diyformid']),
			'diyformtype'=>intval($postdata['diyformtype']),
			//扩展属性
			'labelid'=>intval($postdata['labelid']),
			'isoffcode'=>intval($postdata['isoffcode']),
			'offcode'=>trim($postdata['offcode']),
			'offcodeunit'=>trim($postdata['offcodeunit']),
			
			'groupprice'=>trim($postdata['groupprice']),
			'groupnum'=>trim($postdata['groupnum']),
			'grouptime'=>trim($postdata['grouptime']),
		);
		//获取分类
		$ccate = intval($postdata['cid']['cid']);
		if($ccate){
			$pcate = pdo_getcolumn('jy_weishop_goods_group',array('id'=>$ccate),'parentid');
			$data['ccate']=$ccate;
			$data['pcate']=$pcate;
		}else{
			$pcate = pdo_getcolumn('jy_weishop_goods_group',array('title'=>$postdata['cid']['title'],'uniacid'=>$_SESSION['uniacid']),'id');
			$data['pcate']=$pcate;
		}
		//如果商品类型为计时/计次商品，默认开启核销
		if($data['type']=='3'){
			$data['isverify']=1;
		}
		$onepluginname=$this->model('goods')->is_onegoods($_SESSION['current_plugin']);	
		$goodsid=intval($postdata['id']);
		if($goodsid){
			if(!$postdata['onepluginname']){
				$data['onepluginname']=$onepluginname?:'';
			}
			pdo_update('jy_weishop_goods',$data,array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
			$this->publish('goods.update',array('goodsid'=>$goodsid));
		}else{	
			$data['uniacid']=$_SESSION['uniacid'];
			$data['onepluginname']=$onepluginname?:'';
			pdo_insert('jy_weishop_goods',$data);
			$goodsid=pdo_insertid();
			$this->publish('goods.create',array('goodsid'=>$goodsid));
		}
		return $tags;
	}
	public function date($time){
		$res=array(
			'day'=>0,
			'hour'=>0,
			'minute'=>0,
			'second'=>0,
		);
		
		if($time<=0){
			return $res;
		}
		$res['day']=intval($time/86400);
		$time=$time-$res['day']*86400;
		$res['hour']=intval($time/3600);
		$time=$time-$res['hour']*3600;
		$res['minute']=intval($time/60);
		$res['second']=intval($time-$res['minute']*60);
		return $res;
	}
	public function getExpressList($express, $expresssn)
	{
		$express_set = $this->plugin_setting('express');
		$express_set['cache']=$express_set['cache']?:1;
		$express = ($express == 'jymwl' ? 'jiayunmeiwuliu' : $express);
		$express = ($express == 'TTKD' ? 'tiantian' : $express);
		$express = ($express == 'jjwl' ? 'jiajiwuliu' : $express);
		$express = ($express == 'zhongtiekuaiyun' ? 'ztky' : $express);
		if (!empty($express_set['isopen']) && !empty($express_set['apikey'])) {
			if (!empty($express_set['cache']) && (0 < $express_set['cache'])) {
				$cache_time = $express_set['cache'] * 60;
				$cache = $this->pdo_get('express_cache',array('express' => $express, 'expresssn' => $expresssn));
				if ((time() <= $cache['lasttime'] + $cache_time) && !empty($cache['datas'])) {
					return json_decode($cache['datas'],true);
				}
			}
			if ($express_set['isopen'] == 1) {
				$url = 'http://api.kuaidi100.com/api?id=' . $express_set['apikey'] . '&com=' . $express . '&nu=' . $expresssn;
				$params = array();
			}else {
				$url = 'http://poll.kuaidi100.com/poll/query.do';
				$params = array('customer' => $express_set['customer'], 'param' => ijson_encode(array('com' => $express, 'num' => $expresssn)));
				$params['sign'] = md5($params['param'] . $express_set['apikey'] . $params['customer']);
				$params['sign'] = strtoupper($params['sign']);
			}
			$response = cfc('http')->request($url, $params);
			$content = $response;
			$info = json_decode($content, true);
		}

		if (!isset($info) || empty($info['data']) || !is_array($info['data'])) {
			$url = 'https://www.kuaidi100.com/query?type=' . $express . '&postid=' . $expresssn . '&id=1&valicode=&temp=';
			$response = cfc('http')->request($url);
			$content = $response;
			$info = json_decode($content, true);
			$useapi = false;
		}else {
			$useapi = true;
		}
		$list = array();
		if (!empty($info['data']) && is_array($info['data'])) {
			foreach ($info['data'] as $index => $data) {
				$list[] = array('time' => trim($data['time']), 'step' => trim($data['context']));
			}
		}
		if ((0 < $express_set['cache']) && count($list)>1) {
			if (empty($cache)) {
				pdo_insert('jy_weishop_express_cache', array('expresssn' => $expresssn, 'express' => $express, 'lasttime' => time(), 'datas' => ijson_encode($list)));
			}else {
				pdo_update('jy_weishop_express_cache', array('lasttime' => time(), 'datas' => ijson_encode($list)), array('id' => $cache['id']));
			}
		}
		return $list;
	}
	public function set_tanmu($orderid='',$plugin_name=''){
		$params=array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1);
		if($plugin_name){
			$params['plugin_name']=$plugin_name;
		}
		if($orderid){
			$params['id']=$orderid;
			$order=pdo_get('jy_weishop_order',$params,array('id','mid','paytime'));
			$msgdata['avatar']=pdo_getcolumn('core_members',array('id'=>$order['mid']),'avatar');
			$msgdata['nickname']=pdo_getcolumn('core_members',array('id'=>$order['mid']),'nickname');
			$msgdata['time']=time()-$order['paytime']+2;
			$goods=pdo_getall('jy_weishop_order_goods',$params=array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$orderid),array('id','goodsid'));
			foreach($goods as $g){
				cfc('jinyun_socket')->send2room($msgdata,'goodsid'.$g['goodsid']);
			}
			cfc('jinyun_socket')->send2room($msgdata,'index');
		}else{
			//看看60秒内是否有订单；
			$params['paytime >=']=time()-60;
			$order=pdo_get('jy_weishop_order',$params,array('id','mid','createtime'));
			if($order['id']){
				$msgdata['avatar']=pdo_getcolumn('core_members',array('id'=>$order['mid']),'avatar');
				$msgdata['nickname']=pdo_getcolumn('core_members',array('id'=>$order['mid']),'nickname');
				$msgdata['time']=time()-$order['paytime']+2;
				$goods=pdo_getall('jy_weishop_order_goods',$params=array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$orderid),array('id','goodsid'));
				foreach($goods as $g){
					cfc('jinyun_socket')->send2room($msgdata,'goodsid'.$g['goodsid']);
				}
			}else{
				$xuniuser=pdo_count('core_members',array('uniacid'=>$_SESSION['uniacid'],'is_virtual'=>1),'id');
				$pindex = max(1, intval($post['page']));
				$psize = 1;
				$total='';
				$maxmember=pdo_getslice('core_members',array('uniacid'=>$_SESSION['uniacid'],'is_virtual'=>1),array($pindex,$psize),$total,array('id'),'','id desc');
				$minmember=pdo_getslice('core_members',array('uniacid'=>$_SESSION['uniacid'],'is_virtual'=>1),array($pindex,$psize),$total,array('id'),'','id asc');
				$id=rand($minmember[0]['id'],$maxmember[0]['id']);
				$msgdata['avatar']=pdo_getcolumn('core_members',array('id'=>$id),'avatar');
				$msgdata['nickname']=pdo_getcolumn('core_members',array('id'=>$id),'nickname');
				$msgdata['time']=rand(2,10);
				cfc('jinyun_socket')->send2room($msgdata,'index');
			}
		}
		return true;
	}
	/*
		缴纳押金
	*/
	public function checkauth($merchid=0,$isres=false){
		if($this->setting['yajin_open'] && intval($this->setting['yajin'])>0){
			$is_yajinfee=pdo_getcolumn('jy_supplier_user',array('id'=>$merchid),'is_yajinfee');
			if(!$is_yajinfee || $is_yajinfee<=0){
				if($isres){
					return false;
				}else{
					exi('还未缴纳押金不能开小店！','error','jy_supplier/manage.setyajin/');
				}
			}
		}
		return true;
	}
	/*
		审核供货价
	*/
	public function checkcostprice($item=array()){
		pdo_update('jy_weishop_goods',array('costprice'=>$item['costprice']),array('id'=>intval($item['goodsid']),'uniacid'=>$_SESSION['uniacid']));
		foreach($item['optionsprice'] as $ov){
			pdo_update('jy_weishop_goods_option',array('costprice'=>$ov['costprice']),array('id'=>intval($ov['id']),'uniacid'=>$_SESSION['uniacid']));
		}
		pdo_update('jy_supplier_pricelog',array('status'=>2),array('status'=>0,'goodsid'=>$item['goodsid'],'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
}